; WangWang Fairy v1.1, released under Apache 2.0 license
;
;	http://ww-fairy.googlecode.com/


; Configurate the "off topic" duration here: (in milliseconds)
OFF_TOPIC_DURATION := 600000
OFF_TOPIC_LONG_DURATION := 1800000


count := 0
Menu, Tray, Tip, WangWang Fairy
OnExit, RestoreAllAndExit

; Strip ads
Loop {
	Process, Exist, aliim.exe
	pid := ErrorLevel
	if (pid = 0) {
		Sleep, 10000
		continue
	}

	;SetTitleMatchMode, 2
	Loop {
		; Detect windows containing ad.
		WinWaitActive, ahk_class StandardFrame
    Control, Hide,, CommercialTextLink1
    Control, Hide,, AdvertisementView1    ; Old
    Control, Hide,, CommercialBanner1     ; New
		;ControlGetText, text, Static11
		;if ErrorLevel
		;	Control, Hide,, Static6
		;else
		;	Control, Hide,, Static11
		Sleep, 3000
		IfWinNotExist, ahk_pid %pid%
			break				; WangWang is closed.
	}
}

; Activate main window and focus on the quick search box.
#Q::
	DetectHiddenWindows, On
	SetTitleMatchMode, 2
	WinGet, winid, ID, -`阿`里`旺`旺
	SetTitleMatchMode, 1
	If (winid) {
		WinWait, ahk_id %winid%
		WinShow
		WinActivate
		ControlFocus, EditComponent2, A
		ControlSetText, EditComponent2,, A
	}
	DetectHiddenWindows, Off
	return

#X::
	winid := WinExist("A")
	WinGetTitle, title, ahk_id %winid%
	Schedule(winid, title, OFF_TOPIC_DURATION)
	Gosub, UpdateIconTip
	WinHide, ahk_id %winid%
	TrayTip, Hided, %title%
	SetTimer, RemoveTrayTip, 3000
	return

#^X::
	winid := WinExist("A")
	WinGetTitle, title, ahk_id %winid%
	Schedule(winid, title, OFF_TOPIC_LONG_DURATION)
	Gosub, UpdateIconTip
	WinHide, ahk_id %winid%
	TrayTip, Hided, %title%
	SetTimer, RemoveTrayTip, 3000
	return

RemoveTrayTip:
	SetTimer, RemoveTrayTip, Off
	TrayTip
	return

UpdateIconTip:
	index := 1
	if (count = 0) {
		Menu, Tray, Tip, WangWang Fairy
		return
	}
	tip = %count% Hidden Windows:
	Loop, %count% {
		Loop {
			if (hidden%index% != 0)
				break
			index ++
		}
		winid := hidden%index%
		if (winid = 0)
			continue
		time := time%index%
		SetFormat, float, 0.0
		timeleft := (time - A_TickCount) / 1000
		description := description%index%
		tip = %tip%`n  %description% (%timeleft%s left)
		index ++
	}
	Menu, Tray, Tip, %tip%
	return

Schedule(winid, description, duration) {
	global
	local index := 1
	Loop {
		if (hidden%index% = 0 || hidden%index% = "")
			break
		index ++
	}
	hidden%index% := winid
	description%index% := description
	time%index% := A_TickCount + duration
	count ++
	if (count = 1)
		SetTimer, PeriodicTask, 5000
}

PeriodicTask:
	index := 1
	changed := 0
	Loop, %count% {
		Loop {
			winid := hidden%index%
			if (winid != 0)
				break
			index ++
		}
		if (A_TickCount > time%index%) {
			WinShow, ahk_id %winid%
			hidden%index% := 0
			description%index% :=
			time%index% :=
			count --
			if (count = 0)
			SetTimer, PeriodicTask, Off
			changed := 1
		}
	}
	Gosub, UpdateIconTip
	return

RestoreAllAndExit:
	index := 1
	Loop, %count% {
		Loop {
			winid := hidden%index%
			if (winid != 0)
				break
			index ++
		}
		WinShow, ahk_id %winid%
	}
	ExitApp
